<template>
  <div align="right">
    <el-button type="danger" v-if="currOrder.status.id===5 || currOrder.status.id===7" @click="del">删除订单</el-button>
    <el-button type="danger"
               v-if="currOrder.status.id===0 && !(currOrder.orderGroupStatus && currOrder.orderGroupStatus.id===2)"
               @click="act(5)">取消订单
    </el-button>
    <el-button type="warning" v-if="currOrder.status.id===0 && !currOrder.payTime" @click="pay()">支付订单</el-button>
    <time-out-box :diff-sconds="currOrder.timeOutInfo" :callback="setTimeOut"
                  v-if="currOrder.status.id===0 && !currOrder.payTime && currOrder.timeOutInfo>0"/>
    <el-button type="success"
               v-if="currOrder.sendType.id===2 && currOrder.payType && currOrder.status.id===0 && (!currOrder.orderGroupId || (currOrder.orderGroupId>0 && currOrder.orderGroupStatus.id===2))"
               @click="act(3)">确认自提
    </el-button>
    <el-button type="success" v-if="currOrder.status.id===1 && currOrder.sendType.id!==2" @click="act(3)">确认收货
    </el-button>
    <!--    <el-button type="success" v-if="currOrder.status.id===1 && currOrder.sendType.id!==2 && (!currOrder.orderGroupId || (currOrder.orderGroupId>0 && currOrder.orderGroupStatus.id===2))" @click="act(3)">确认收货</el-button>-->
    <el-button type="warning" v-if="currOrder.status.id===3" @click="reply()">评价订单</el-button>
    <el-button type="warning" v-if="currOrder.status.id===4" @click="reply('show')">查看评价</el-button>
    <el-button type="primary"
               v-if="currOrder.isEnableSubmit && (currOrder.status.id===3 || currOrder.status.id===4) && currOrder.applyId===0"
               @click="apply()">退换货
    </el-button>
    <el-button type="primary" v-if="(currOrder.status.id===3 || currOrder.status.id===4) && currOrder.applyId>0"
               @click="apply()">查看退换货
    </el-button>
  </div>
</template>

<script>
import TimeOutBox from "./TimeOutBox";

export default {
  name: 'OrderActButton',
  components: {TimeOutBox},
  props: {
    currOrderParams: Object,
    callBack: Function
  },
  data: function () {
    return {
      currOrder: {
        id: 0,
        status: {
          id: -1
        },
        sendType: {},
        payType: {},
        orderGroupStatus: {}
      }
    }
  },
  mounted() {
    this.currOrder = this.currOrderParams
  },
  methods: {
    setTimeOut: function () {
      this.$set(this.currOrder,'status',{
        id:7,
        name:'超时取消'
      })
      this.$forceUpdate()
    },
    // 售后(退换货)
    apply: function () {
      var query = {
        orderId: this.currOrder.id,
      }
      if (this.currOrder.applyId === 0) {
        query.isInit = 1
      } else {
        query.applyId = this.currOrder.applyId
      }
      this.$router.push({
        name: 'OrderApply',
        query: query
      })
    },
    pay: function (order) {
      this.bus.$emit('ChooseOrderPayTypeDialogCallBus', this.currOrder)
    },
    // 订单操作: 取消订单,确认收货
    act: function (act) {
      let $this = this
      var actRequest = function (act, reason) {
        var form = {id: $this.currOrder.id, status: act}
        if (act === 5) {
          form.cancelReason = reason
        }
        $this.myLoading = true
        $this.MyRequest($this.ApiDoNameMember + 'Order/act', form, function (data) {
          $this.myLoading = false
          $this.$props.callBack()
          $this.$forceUpdate()
        }, function (msg) {
          $this.myLoading = false
          $this.$message.error(msg)
        })
      }

      if (act === 5) {
        $this.$prompt('请输入 取消原因', '系统提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputValue: '不想要了',
          inputErrorMessage: '取消原因错误格式不正确'
        }).then(({value}) => {
          if (value.length < 3) {
            return $this.successTip('取消原因必须于3个字符')
          }
          actRequest(act, value)
        })
      } else {
        $this.$confirm('您确认要进行该操作吗, 是否继续?', '系统提示', {
          customClass: 'my-confirm-box',
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          backType: 'warning'
        }).then(() => {
          actRequest(act)
        }).catch(() => {

        })
      }
    },
    del: function () {
      let $this = this
      $this.$confirm('您确认要进行该操作吗, 是否继续?', '系统提示', {
        customClass: 'my-confirm-box',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        backType: 'warning'
      }).then(() => {
        $this.myLoading = true
        $this.MyRequest($this.ApiDoNameMember + 'Order/delete', {
          id: $this.currOrder.id
        }, function (data) {
          $this.$message.success('删除成功')
          $this.myLoading = false
          if ($this.$route.path.indexOf('OrderDetail') > -1) {
            $this.$router.push('OrderList')
          } else {
            $this.$props.callBack()
          }
          $this.$forceUpdate()
        }, function (msg) {
          $this.myLoading = false
          $this.$message.error(msg)
        })
      }).catch(() => {

      })
    },
    // 订单评价
    reply: function (type) {
      var query = {
        id: this.currOrder.id
      }
      if (type) {
        query.type = type
      }
      this.$router.push({
        name: 'OrderReply',
        query: query
      })
    }
  }
}
</script>

<style scoped>

</style>
